//
//  DJMyWallApplyVC.m
//  daojiVideo
//
//  Created by adam on 2024/6/10.
//

#import "DJMyWallApplyVC.h"
#import "DJMyWallApplyHeaderView.h"
#import "DJMyWallApplyCell.h"
#import "DJMyWallAcountBindVC.h"
#import "DJMyWallRecordVC.h"
#import "DJMyWallRecordVC.h"

@interface DJMyWallApplyVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIButton *tixianButton;
@property (nonatomic, strong) NSString * seleteRow;
@property (weak, nonatomic) IBOutlet UILabel *tixianLabel;


@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSDictionary *doneAccount,*moneyDic;
@end

@implementation DJMyWallApplyVC
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestDatas];
}
-(void)showTextViewNavViewTo1{
    NSMutableArray * showTextViewNavViewNameArrTo1 = [NSMutableArray new];
    UIView * showTextViewNavViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewNavViewViewTo1.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewNavViewViewTo1];
    UILabel * showTextViewNavViewLabelTo1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    NSInteger showTextViewNavViewNumberTo1 = arc4random_uniform(20);
    NSLog(@"：%ld", (long)showTextViewNavViewNumberTo1);
    showTextViewNavViewLabelTo1.text = [NSString stringWithFormat:@"%ld",(long)showTextViewNavViewNumberTo1];
    [showTextViewNavViewLabelTo1 addSubview:showTextViewNavViewLabelTo1];
    
    UIImageView * showTextViewNavViewLabelTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [showTextViewNavViewViewTo1 addSubview:showTextViewNavViewLabelTo2];
    
}
- (void)showOldAddNavViewToMainOldView1{
    UIView * oldBgSS = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    oldBgSS.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:oldBgSS];
    
    UIButton * sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-75, kStatusBarHeight+10, 60, 46)];
    sendBtn.layer.cornerRadius = 3;
    sendBtn.backgroundColor = [UIColor whiteColor];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:ColorHex(@"#333333") forState:UIControlStateNormal];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendBtn addTarget:self action:@selector(sendTap) forControlEvents:UIControlEventTouchUpInside];
    [oldBgSS addSubview:sendBtn];
    
    UIView * sendBg = [[UIView alloc]initWithFrame:CGRectMake(15, kStatusBarHeight+66, KScreen_W-30, (KScreen_H-KSafeAreaBottomHeight-25-(kStatusBarHeight+66))/2)];
    sendBg.backgroundColor = [UIColor whiteColor];
    sendBg.layer.cornerRadius = 3;
    [oldBgSS addSubview:sendBg];
    
    UILabel * sendTip = [[UILabel alloc]initWithFrame:CGRectMake(0, (sendBg.frame.size.height-40)/2, sendBg.frame.size.width, 40)];
    sendTip.text = @"发送区";
    sendTip.textColor = ColorHex(@"#f0f0f0");
    sendTip.textAlignment = NSTextAlignmentCenter;
    sendTip.font = [UIFont systemFontOfSize:28];
    [sendBg addSubview:sendTip];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"提现钱包";
    if([[QZUserInfoModel share].iscreator intValue] == 1){
        self.tixianLabel.hidden = NO;
    }else{
        self.tixianLabel.hidden = YES;
    }
    self.tixianButton.backgroundColor = MainColor;
    [self.collectionView registerNib:[UINib nibWithNibName:@"DJMyWallApplyCell" bundle:nil] forCellWithReuseIdentifier:@"DJMyWallApplyCell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"DJMyWallApplyHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"DJMyWallApplyHeaderView"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.tixianButton.layer.cornerRadius = 8;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提现明细"
                                                                              style:UIBarButtonItemStyleDone
                                                                             target:self
                                                                             action:@selector(detailTap)];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName : ColorHex_Alpha(@"FFFFFF", 0.82)}
                                                          forState:UIControlStateNormal];
}
- (void)detailTap{
    DJMyWallRecordVC * detail = [[DJMyWallRecordVC alloc]init];
    [self.navigationController pushViewController:detail animated:YES];
}
- (IBAction)tixiangTap:(id)sender {
    KMutableDic(sendDic)
    if(self.seleteRow.length == 0){
        [YBMsgPop showPop:@"选择提现金额"];
        return;
    }
    int index = [self.seleteRow intValue];
    if([self.moneyDic[@"wallet_account"][@"total_amount"] floatValue] < [self.dataSource[index][@"money"] floatValue]){
        [YBMsgPop showPop:@"提现金额不足"];
        return;
    }
    sendDic[@"money"] = self.dataSource[index][@"money"];
    sendDic[@"accountid"] = self.moneyDic[@"accountData"][0][@"id"];
    KWeakSelf
    [YBNetBase cashAccountRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            [weakSelf.navigationController popViewControllerAnimated:YES];
            [YBMsgPop showPop:@"提现申请成功，请等待!!"];
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}

- (void)requestDatas{
    KWeakSelf
    KMutableDic(sendDic)
    [YBNetBase memberAccountMoneyListRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            if(weakSelf.moneyDic == nil){
                weakSelf.moneyDic = data;
                [weakSelf.dataSource addObjectsFromArray:weakSelf.moneyDic[@"moneyData"]];
                [weakSelf.collectionView reloadData];
            }else{
                weakSelf.moneyDic = data;
                [weakSelf.collectionView reloadData];
            }
            
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}
#pragma mark - UICollectionView data source
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    DJMyWallApplyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DJMyWallApplyCell" forIndexPath:indexPath];
    NSDictionary *tempData = self.dataSource[indexPath.row];
    cell.moneyLabel.text = [NSString stringWithFormat:@"%@%@",tempData[@"money"],tempData[@"ms"]];
    if(self.seleteRow.length > 0 && [self.seleteRow intValue] == indexPath.row){
        cell.backgroundColor = MainColor;
    }
    else{
        cell.backgroundColor = subviewback_black;
    }
    
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(KScreen_W, 220);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        DJMyWallApplyHeaderView * header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"DJMyWallApplyHeaderView" forIndexPath:indexPath];
        header.moneLabel.text = [NSString stringWithFormat:@"%0.2f",[self.moneyDic[@"wallet_account"][@"total_amount"] floatValue]];
        if(self.moneyDic[@"accountData"] == nil || [self.moneyDic[@"accountData"] count] == 0){
            header.acountName.text = @"未设置提现账号";
        }else{
            header.acountName.text = self.moneyDic[@"accountData"][0][@"accounts"];
        }
        KWeakSelf
        header.MyWallApplyHeaderBlock = ^(NSInteger index) {
            if(index == 0){
                DJMyWallAcountBindVC * bind = [[DJMyWallAcountBindVC alloc]init];
                if([weakSelf.moneyDic[@"accountData"] count] > 0){
                    bind.accountDic = weakSelf.moneyDic[@"accountData"][0];
                }
                [weakSelf.navigationController pushViewController:bind animated:YES];
            }
        };
        return header;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = (KScreen_W - 50)/3;
    return CGSizeMake(width, 64);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                        layout:(UICollectionViewLayout *)collectionViewLayout
        insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(15, 15, 15, 15);
}

- (void)showTextViewSubViewToViewTo1:(UIView * )subView{

    UIView * showTextViewSubViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewSubViewTo1.clipsToBounds = YES;
    [subView addSubview:showTextViewSubViewTo1];
    UILabel * showTextViewSubViewLabelTo2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    showTextViewSubViewLabelTo2.text = @"56464645";
    [showTextViewSubViewTo1 addSubview:showTextViewSubViewLabelTo2];
    
    UIImageView * showTextViewSubViewTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [subView addSubview:showTextViewSubViewTo2];

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.seleteRow = [NSString stringWithFormat:@"%ld",indexPath.row];
    [collectionView reloadData];
}

- (NSMutableArray *)dataSource{
    if(!_dataSource){
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}
-(void)showTextViewAddShowViewTo2{
    UIView * showTextViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 20, 100, 360)];
    showTextViewViewTo1.backgroundColor = [UIColor whiteColor];
    showTextViewViewTo1.layer.cornerRadius = 8;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewViewTo1];
    
    UIImageView * showTextViewimgTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(K_AUTOLAYOUT_W(157.5), K_AUTOLAYOUT_W(260), K_AUTOLAYOUT_W(60), K_AUTOLAYOUT_W(60))];
    showTextViewimgTo2.layer.cornerRadius = K_AUTOLAYOUT_W(30);
    showTextViewimgTo2.clipsToBounds = YES;
    showTextViewimgTo2.contentMode = UIViewContentModeScaleAspectFill;
    showTextViewimgTo2.hidden = YES;
    [showTextViewViewTo1 addSubview:showTextViewimgTo2];
    
    UILabel *  showTextViewimgTo3 = [[UILabel alloc]initWithFrame:CGRectMake(K_AUTOLAYOUT_W(15), K_AUTOLAYOUT_W(35), K_AUTOLAYOUT_W(345), K_AUTOLAYOUT_W(25))];
    showTextViewimgTo3.font = FontBold(18);
    showTextViewimgTo3.textColor = ThreeColor;
    showTextViewimgTo3.textAlignment = NSTextAlignmentCenter;
    showTextViewimgTo3.hidden = YES;
    [showTextViewViewTo1 addSubview:showTextViewimgTo3];
}
- (NSString *)showTextViewGetNowTimerTo3{
    NSDateFormatter *showTextViewGetNowFa = [[NSDateFormatter alloc] init];
    [showTextViewGetNowFa setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [showTextViewGetNowFa stringFromDate:[NSDate date]];
}
@end
